<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #d1{
            width: 150px;
            height:150px;
            background-color: red;
            position: absolute;
            left:0;
            top:100;

        }
    </style>
</head>
<body>
    <input id="startBtn" type="button" value="开始">
    <input id="endBtn" type="button" value="停止">
    <div id="d1">

    </div>
    <script>
        let startBtn = document.getElementById("startBtn");
        let d1 = document.getElementById("d1");
        let x = 1;
        let y = 1;
        let intervalId;
        let isStart = false;
        startBtn.onclick = function(){
            if(isStart){
                return;
            }
            intervalId = setInterval(() => {
                d1.style.left = parseInt(d1.style.left || 0) + (15 * x) + "px";
                d1.style.top = parseInt(d1.style.top || 0) + (15 * y) + "px";
                if(parseInt(d1.style.left) >= innerWidth - 150 || parseInt(d1.style.left) <= 0){
                    x *= -1;
                }
                if(parseInt(d1.style.top) >= innerHeight - 150 || parseInt(d1.style.top) <= 0){
                    y *= -1;
                }
            },50)
            isStart = true;
        }
        let endBtn = document.getElementById("endBtn");
        endBtn.onclick = function(){
            clearInterval(intervalId);
            isStart = false;
        }
    </script>
</body>
</html>